﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Purdue.Infrastructure;
using Purdue.Models;

namespace Purdue.ViewModel
{
    public class CustomerInviteCodeViewModel : ViewModelBase
    {
        public void Generate(int count, string agentCode)
        {
            IList<CustomerInviteCode> codes = new List<CustomerInviteCode>();
            Int64 max = Database.CollectionOfCustomerInviteCodes.OrderByDescending(e => e.Id).Count(e => e.ServiceCode.Length > 5);
            if (max < 100000)
            {
                for (int i = 1; i <= count; i++)
                {
                    Int64 service = 100000 + max + i;
                    codes.Add(new CustomerInviteCode()
                    {
                        ServiceCode = service.ToString(),
                        Password = new Library.RandomPassword().Generate(6),
                        Code = string.Format("{0}-{1}{2}", agentCode, DateTime.Now.ToString("MMdd"), new Random().Next(100000).ToString().PadLeft(5, '0')),
                        AgentCode = agentCode
                    });
                    i++;
                }
            }
            else
            {
                for (int i = 1; i <= count; i++)
                {
                    Int64 service = max + i;
                    codes.Add(new CustomerInviteCode() { ServiceCode = service.ToString(), Password = new Library.RandomPassword().Generate(6), Code = string.Format("{0}-{1}{2}", agentCode, DateTime.Now.ToString("MMdd"), new Random().Next(100000).ToString().PadLeft(5, '0')), AgentCode = agentCode });
                    i++;
                }
            }

            Database.CollectionOfCustomerInviteCodes.InsertAllOnSubmit(codes);
            Save();
        }
    }
}
